package com.itheima.protocol.javaApi;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class CreateSession {

    private final static String CONNECTIONSTRING="192.168.1.108:2181";
    private final static int SESSIONTIMEOUT = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {

        ZooKeeper zooKeeper = new ZooKeeper(CONNECTIONSTRING, SESSIONTIMEOUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                    System.out.println(watchedEvent.getState());
                }

            }
        });

        countDownLatch.await();
        System.out.println(zooKeeper.getState());
    }
}
